package com.nativetalk.controller.app.four.teacher.label;

import com.nativetalk.base.RetInfo;
import com.nativetalk.bean.TdNtTeacherLabel;
import com.nativetalk.bean.teacher.TdNtTeacherResult;
import com.nativetalk.common.Constants;
import com.nativetalk.common.Lang;
import com.nativetalk.common.util.JsonUtil;
import com.nativetalk.common.util.MemcachedUtils;
import com.nativetalk.service.four.label.FourLabelService;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * Created by lizhun on 15/12/9.
 */
@Controller
public class FourTeacherLabelController {
    private static Logger logger = Logger.getLogger(FourTeacherLabelController.class);
    @Resource
    private FourLabelService fourLabelService;

    /**
     * @return RetInfo
     * @Purpose 查找标签
     * @version 4.0
     * @time 2016-11-3
     * @author lizhun
     */
    @RequestMapping(value = "/app/10/teatoken/label/list", method = {RequestMethod.GET, RequestMethod.POST}, consumes = "application/json")
    @ResponseBody
    public RetInfo list(@RequestHeader(value = "token", required = false) String token) {
        try {
            //打印入参
            logger.info(Constants.prefix_in);

            //校验token，如果校验通过就从缓存中读取Teacher
            if (StringUtils.isBlank(token)) {
                RetInfo retInfo = RetInfo.getClientErrorInfo("token is required");
                logger.info(JsonUtil.toJson(retInfo));
                return retInfo;
            }
            Object o = MemcachedUtils.get(token);
            if (o == null) {
                RetInfo retInfo = RetInfo.getReloginInfo(Lang.EN);
                logger.info("缓存中没有teatoken:[" + token + "]，用户需要重新登录系统。 retInfo-->" + JsonUtil.toJson(retInfo));
                return retInfo;
            }

            if (!TdNtTeacherResult.class.isInstance(o)) {
                RetInfo retInfo = RetInfo.getReloginInfo(Lang.EN);
                logger.info("缓存的token不是TdNtTeacherResult类型:[" + token + "]，用户需要重新登录系统。 retInfo-->" + JsonUtil.toJson(retInfo));
                return retInfo;
            }
            TdNtTeacherResult tdNtTeacherResult = (TdNtTeacherResult) o;

            RetInfo retInfo = fourLabelService.findTeacherLabel(tdNtTeacherResult.getTea_id());

            //打印出参
            logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        } catch (Exception e) {
            //记录错误日志
            RetInfo retInfo = RetInfo.getServerErrorInfo();
            logger.error(e.getMessage(), e);

            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        }
    }

    /**
     * @return RetInfo
     * @Purpose 添加标签
     * @version 4.0
     * @time 2016-11-3
     * @author lizhun
     */
    @RequestMapping(value = "/app/10/teatoken/label/add", method = {RequestMethod.GET, RequestMethod.POST}, consumes = "application/json")
    @ResponseBody
    public RetInfo add(@RequestBody TdNtTeacherLabel tdNtTeacherLabel, @RequestHeader(value = "token", required = false) String token) {
        try {
            //打印入参
            logger.info(Constants.prefix_in + JsonUtil.toJson(tdNtTeacherLabel));

            //校验token，如果校验通过就从缓存中读取Teacher
            if (StringUtils.isBlank(token)) {
                RetInfo retInfo = RetInfo.getClientErrorInfo("token is required");
                logger.info(JsonUtil.toJson(retInfo));
                return retInfo;
            }
            Object o = MemcachedUtils.get(token);
            if (o == null) {
                RetInfo retInfo = RetInfo.getReloginInfo(Lang.EN);
                logger.info("缓存中没有teatoken:[" + token + "]，用户需要重新登录系统。 retInfo-->" + JsonUtil.toJson(retInfo));
                return retInfo;
            }

            if (!TdNtTeacherResult.class.isInstance(o)) {
                RetInfo retInfo = RetInfo.getReloginInfo(Lang.EN);
                logger.info("缓存的token不是TdNtTeacherResult类型:[" + token + "]，用户需要重新登录系统。 retInfo-->" + JsonUtil.toJson(retInfo));
                return retInfo;
            }
            TdNtTeacherResult tdNtTeacherResult = (TdNtTeacherResult) o;


            tdNtTeacherLabel.setTea_id(tdNtTeacherResult.getTea_id());
            tdNtTeacherLabel.setState(0);

            RetInfo retInfo = fourLabelService.add(tdNtTeacherLabel);

            //打印出参
            logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        } catch (Exception e) {
            //记录错误日志
            RetInfo retInfo = RetInfo.getServerErrorInfo();
            logger.error(e.getMessage(), e);

            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        }
    }

    /**
     * @return RetInfo
     * @Purpose 修改标签
     * @version 4.0
     * @time 2016-11-3
     * @author lizhun
     */
    @RequestMapping(value = "/app/10/teatoken/label/update", method = {RequestMethod.GET, RequestMethod.POST}, consumes = "application/json")
    @ResponseBody
    public RetInfo update(@RequestBody TdNtTeacherLabel tdNtTeacherLabel, @RequestHeader(value = "token", required = false) String token) {
        try {
            //打印入参
            logger.info(Constants.prefix_in + JsonUtil.toJson(tdNtTeacherLabel));

            //打印入参

            //校验token，如果校验通过就从缓存中读取Teacher
            if (StringUtils.isBlank(token)) {
                RetInfo retInfo = RetInfo.getClientErrorInfo("token is required");
                logger.info(JsonUtil.toJson(retInfo));
                return retInfo;
            }
            Object o = MemcachedUtils.get(token);
            if (o == null) {
                RetInfo retInfo = RetInfo.getReloginInfo(Lang.EN);
                logger.info("缓存中没有teatoken:[" + token + "]，用户需要重新登录系统。 retInfo-->" + JsonUtil.toJson(retInfo));
                return retInfo;
            }

            if (!TdNtTeacherResult.class.isInstance(o)) {
                RetInfo retInfo = RetInfo.getReloginInfo(Lang.EN);
                logger.info("缓存的token不是TdNtTeacherResult类型:[" + token + "]，用户需要重新登录系统。 retInfo-->" + JsonUtil.toJson(retInfo));
                return retInfo;
            }
            TdNtTeacherResult tdNtTeacherResult = (TdNtTeacherResult) o;

            RetInfo retInfo = fourLabelService.udpate(tdNtTeacherLabel);

            //打印出参
            logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        } catch (Exception e) {
            //记录错误日志
            RetInfo retInfo = RetInfo.getServerErrorInfo();
            logger.error(e.getMessage(), e);

            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        }
    }

    /**
     * @return RetInfo
     * @Purpose 修改标签
     * @version 4.0
     * @time 2016-11-3
     * @author lizhun
     */
    @RequestMapping(value = "/app/10/teatoken/label/delete", method = {RequestMethod.GET, RequestMethod.POST}, consumes = "application/json")
    @ResponseBody
    public RetInfo delete(@RequestBody TdNtTeacherLabel tdNtTeacherLabel) {

        try {
            //打印入参
            logger.info(Constants.prefix_in + JsonUtil.toJson(tdNtTeacherLabel));

            RetInfo retInfo = fourLabelService.delete(tdNtTeacherLabel);

            //打印出参
            logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        } catch (Exception e) {
            //记录错误日志
            RetInfo retInfo = RetInfo.getServerErrorInfo();
            logger.error(e.getMessage(), e);

            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        }
    }
}


